package com.mfh.auto.create.mvvm.autocreatemvvm.file.hm

import com.intellij.psi.PsiDirectory
import com.mfh.auto.create.mvvm.autocreatemvvm.Config
import com.mfh.auto.create.mvvm.autocreatemvvm.file.BaseFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.ConstantEdit
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CommonUtils

/**
 * 鸿蒙弹窗文件
 */
object HMDialogFileModel : BaseFileModel() {

    override fun buildFileName() = "${Config.name}Dialog.ets"

    override fun buildContentString(directory: PsiDirectory): String {
        val name = Config.name
        val routeKey = "DIALOG_" + CommonUtils.camelToUnderscore(name).uppercase()
        ConstantEdit.addConstant(
            directory,
            "  // ${Config.desc}\n" +
                    "  public static readonly $routeKey = '${routeKey.lowercase()}'"
        )
        return buildString {
            appendLine(
                "import { Route, ZRouter } from '@hzw/zrouter';\n" +
                        "import { RouterPathConstant } from 'library';\n" +
                        "import { ${name}DialogModel } from './${name}DialogModel';" +
                        "\n" +
                        "/**\n" +
                        " * @desc: ${Config.desc}\n" +
                        " */" +
                        "@Route({ name: RouterPathConstant.${routeKey} })\n" +
                        "@ComponentV2\n" +
                        "export struct ${name}Dialog {\n" +
                        "  @Local\n" +
                        "  viewModel: ${name}DialogModel = new ${name}DialogModel()\n" +
                        "\n" +
                        "  // 展示方法\n" +
                        "  static show(viewModel: ${name}DialogModel) {\n" +
                        "    ZRouter.getInstance()\n" +
                        "      .setParam(viewModel)\n" +
                        "      .navigation(RouterPathConstant.${routeKey})\n" +
                        "  }\n" +
                        "\n" +
                        "  aboutToAppear(): void {\n" +
                        "    this.viewModel = ZRouter.getInstance().getParam() as ${name}DialogModel\n" +
                        "  }\n" +
                        "\n" +
                        "  build() {\n" +
                        "    NavDestination() {\n" +
                        "      Column() {\n" +
                        "        Column() {\n" +
                        "        // todo: \n" +
                        "        }\n" +
                        "        .backgroundColor(Color.White)\n" +
                        "        .onClick(() => {\n" +
                        "        })\n" +
                        "      }\n" +
                        "      .justifyContent(FlexAlign.Center)\n" +
                        "      .width('100%')\n" +
                        "      .height('100%')\n" +
                        "      .onClick(() => {\n" +
                        "        this.viewModel.closeDialog()\n" +
                        "      })\n" +
                        "    }\n" +
                        "    .hideTitleBar(true)\n" +
                        "    .onAppear(() => {\n" +
                        "      let vm = this.viewModel\n" +
                        "      this.viewModel.self = vm\n" +
                        "      this.viewModel.onAppear()\n" +
                        "    })\n" +
                        "    .onShown(() => this.viewModel.onShow())\n" +
                        "    .onHidden(() => this.viewModel.onHide())\n" +
                        "    .onDisAppear(() => this.viewModel.onDisappear())\n" +
                        "    .mode(NavDestinationMode.DIALOG)\n" +
                        "    .backgroundColor(\$r('app.color.mask_color'))\n" +
                        "  }\n" +
                        "}"
            )
        }
    }
}